package com.example.harmonyos_ui.slice;

import com.example.harmonyos_ui.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;


public class LoginUiSlice extends AbilitySlice {
    // 隐藏元素
    private Text phone_error_text = null;
    private Text passwd_error_text = null;
    private Text phone_correct_text = null;
    private Text passwd_correct_text = null;
    // 设定手机号密码
    private String phone = "123456";
    private String passwd = "123456";
    // 文本框
    private TextField phone_text_field = null;
    private TextField passwd_text_field = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_login_ui);
        // 获取隐藏元素
        phone_error_text = (Text)findComponentById(ResourceTable.Id_error_login_ui_phone_textfield);
        passwd_error_text = (Text)findComponentById(ResourceTable.Id_error_login_ui_passwd_textfield);
        phone_correct_text = (Text)findComponentById(ResourceTable.Id_correct_login_ui_phone_textfield);
        passwd_correct_text = (Text)findComponentById(ResourceTable.Id_correct_login_ui_passwd_textfield);
        // 获取布局
        DirectionalLayout login_ui_layout = (DirectionalLayout)findComponentById(ResourceTable.Id_login_ui_layout);
        // 绑定布局事件
        login_ui_layout.setClickedListener(this::clickLayout);
        // 获取文本框
        phone_text_field = (TextField)findComponentById(ResourceTable.Id_login_ui_phone_textfield);
        passwd_text_field = (TextField)findComponentById(ResourceTable.Id_login_ui_passwd_textfield);
        // 获取按钮
        Button login_button = (Button)findComponentById(ResourceTable.Id_login_ui_button) ;
        Button sign_up_button = (Button)findComponentById(ResourceTable.Id_sign_up_ui_button);
        // 绑定按钮事件
        login_button.setClickedListener(this::pushLoginButton);
        sign_up_button.setClickedListener(this::pushSignUpButton);
    }

    // 窗体被点击
    private void clickLayout(Component component) {
        // 获取正常状态下的样式
        ShapeElement normalElement = new ShapeElement(this, ResourceTable.Graphic_background_textfield);
        // 还原隐藏元素
        phone_error_text.setVisibility(Component.HIDE);
        passwd_error_text.setVisibility(Component.HIDE);
        phone_correct_text.setVisibility(Component.HIDE);
        passwd_correct_text.setVisibility(Component.HIDE);
        // 还原提示
        phone_text_field.setHint("请输入手机号");
        passwd_text_field.setHint("请输入密码");
        // 还原文本框样式
        phone_text_field.setBackground(normalElement);
        passwd_text_field.setBackground(normalElement);
    }

    private void setErrorTextField(int flag) {
        // 获取错误状态下的样式
        ShapeElement errorElement = new ShapeElement(this, ResourceTable.Graphic_background_textfield_error);
        // 显示隐藏元素，隐藏提示
        if (flag == 1) {
            phone_error_text.setVisibility(Component.VISIBLE);
            phone_text_field.setBackground(errorElement);
            phone_text_field.setHint("");
            // phone_text_field.setText(String.valueOf(ResourceTable.String_phone));
        } if (flag == 2) {
            passwd_error_text.setVisibility(Component.VISIBLE);
            passwd_text_field.setBackground(errorElement);
            passwd_text_field.setHint("");
            // passwd_text_field.setText(String.valueOf(ResourceTable.String_passwd));
        } else {
            return ;
        }
    }

    private void setCorrectTextField() {
        // 获取错误状态下的样式
        ShapeElement correctElement = new ShapeElement(this, ResourceTable.Graphic_background_textfield_correct);
        // 设置正确隐藏元素
        phone_correct_text.setVisibility(Component.VISIBLE);
        passwd_correct_text.setVisibility(Component.VISIBLE);
        // 设置正确文本框样式
        phone_text_field.setBackground(correctElement);
        passwd_text_field.setBackground(correctElement);
    }

    // 按钮被点击
    private void pushLoginButton(Component component) {
        // 判断输入是否正确
        String input_phone = phone_text_field.getText();
        String input_passwd = passwd_text_field.getText();
        if (!input_phone.equals(phone)) {
            setErrorTextField(1);
        } if (!input_passwd.equals(passwd)) {
            setErrorTextField(2);
        } else {
            setCorrectTextField();
        }
    }

    private void pushSignUpButton(Component component) {

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
